package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.view.widget.lease.common.CommonBaseAdapter;
import com.sgb.kjwl.view.widget.lease.common.CommonItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class PickFilterSecondWindowAdapter extends CommonBaseAdapter<PickFilterEntity, CommonItemViewHolder> {
    private List<PickFilterEntity> mChooseList = new ArrayList<>();

    public PickFilterSecondWindowAdapter(Context context) {
        super(context);
    }

    @Override
    protected CommonItemViewHolder onCreateViewHolderImpl(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pick_filter_child_window, parent, false);
        return new CommonItemViewHolder(view);
    }

    @Override
    protected void onBindViewHolderImpl(CommonItemViewHolder holder, int position) {
        PickFilterEntity item = getItem(position);
        holder.setBinding(BR.viewModel, item);
        TextView picker = holder.itemView.findViewById(R.id.tv_selected);
        LinearLayout root = holder.itemView.findViewById(R.id.ll_root);
        root.setSelected(item.isChoose());
        picker.setSelected(true);
                picker.setVisibility(item.isChoose() ? View.VISIBLE : View.GONE);
        if (mChooseList.contains(item)){
            picker.setVisibility(View.VISIBLE);
            root.setSelected(true);
        }
    }

    @Override
    protected void onItemClick(CommonItemViewHolder holder, int position) {
        super.onItemClick(holder, position);
        List<PickFilterEntity> data = getData();
        if (data == null || data.isEmpty()){
            return;
        }
        PickFilterEntity currentItem = getItem(position);
        for (PickFilterEntity item : data) {
            if (currentItem.equals(item)){
                item.setChoose(!item.isChoose());
                if (item.isChoose() && !mChooseList.contains(item)){
                    mChooseList.add(item);
                }
                if (!item.isChoose() && mChooseList.contains(item)){
                    mChooseList.remove(item);
                }
            }
        }
        notifyDataSetChanged();
    }

    public List<PickFilterEntity> getChooseList() {
        return mChooseList;
    }

    public void resetChooseList(){
        mChooseList.clear();
    }
}
